package com.king.base.rest.redis;
import com.king.base.aop.Log;
import com.king.base.domain.redis.RedisVo;
import com.king.base.service.redis.RedisService;
import com.king.base.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author TianYz
 */
@RestController
@RequestMapping("api/monitor/redis")
@Api(value = "redis",description = "Redis缓存Contoller")
public class RedisController {

    @Autowired
    private RedisService redisService;

    @Log(description = "查询Redis缓存")
    @ApiOperation(value = "redis缓存分页查询")
    @GetMapping(value = "/page")
    public ResponseEntity getRedis(String key, Pageable pageable){
        return new ResponseEntity(PageUtil.toPage(redisService.findByKey(key,pageable)), HttpStatus.OK);
    }
    @Log(description = "删除Redis缓存")
    @ApiOperation(value = "删除单个Redis缓存")
    @DeleteMapping(value = "/delete")
    public ResponseEntity delete(@RequestBody RedisVo resources){
        redisService.delete(resources.getKey());
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log(description = "清空Redis缓存")
    @ApiOperation(value = "清空Redis缓存")
    @DeleteMapping(value = "/deleteAll")
    public ResponseEntity deleteAll(){
        redisService.flushdb();
        return new ResponseEntity(HttpStatus.OK);
    }
}
